package com.bjpowernode.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjpowernode.mapper.LoginLogMapper;
import com.bjpowernode.domain.LoginLog;
import com.bjpowernode.service.LoginLogService;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {
    @Resource
    private LoginLogMapper loginLogMapper;

    /**
     * 日志添加业务方法
     * 增删改 都需要打印日志
     * 查询最好也打印日志
     *
     * @param type     成功1|失败0
     * @param username 用户名
     */
    @Override
    public void addLog(Integer type, String username) {
        System.out.println("type = " + type);
        log.info("记录用户的登录日志,用户名为{}", username);
        LoginLog loginLog = LoginLog.builder()
                .type("密码登录")
                .createTime(new Date())
                .message(type == 1 ? "登录成功" : "登录失败")
                .username(username)
                .status(type)
                .build();
        loginLogMapper.insert(loginLog);
    }
}
